package com.zhongkai.web.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;

public class CharsetFilter extends HttpServlet implements Filter {

	private FilterConfig config = null;
	private String defaultEncode = "UTF-8";

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain Chain) throws IOException, ServletException {
		response.setContentType("text/html;charset="+defaultEncode);
		request.setCharacterEncoding(defaultEncode);
		response.setCharacterEncoding(defaultEncode);
		Chain.doFilter(request, response);
	}

	public void init(FilterConfig arg0) throws ServletException {
		this.config = arg0;
		if (config.getInitParameter("Charset") != null) {
			defaultEncode = config.getInitParameter("Charset");
		}

	}

}
